package sc.common.test.velocity;

import java.util.HashMap;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sc.common.test.VelocityUtil;

public class VeloTest1 {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    public void getString(String template, HashMap<String, Object> paramMap) {
        logger.info("Template[{}]", template);
        logger.info("ParamMap[{}]", paramMap);
        String rst = VelocityUtil.getString(template, paramMap);
        logger.info("Result[{}]", rst);
    }

    /**
     * 模板内可改变传入对象
     */
    @Test
    public void changeParamTest() {
        HashMap<String, String> xxs = new HashMap<String, String>();
        xxs.put("abc", "123");
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("xxs", xxs);
        getString("abc = ${xxs.get('abc')}", paramMap);
        getString("abc org = ${xxs.put('abc', '456')}", paramMap);
        Assert.assertEquals(xxs.get("abc"), "456");
    }

    public static enum Type {
        A,B
    }

    @Test
    public void test() {
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("type", Type.A);
        paramMap.put("str", "A");
        paramMap.put("typeNull", null);
        getString("#set($dic = ['A', 'B'])$dic.contains($str)", paramMap);
        getString("#set($dic = ['A', 'B'])$dic.contains(\"$type\")", paramMap);
        getString("#set($dic = ['A', 'B'])$dic.contains(\"$typeNull\")", paramMap);
    }

    @Test
    public void test2() {
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("str", null);
        paramMap.put("str2", "abc");
        getString("#if($str)str-true#{else}str-false#end", paramMap);
        getString("#if($str2)str2-true#{else}str2-false#end", paramMap);
        getString("#set($rst = \"$str\")$rst", paramMap);

        getString("$!rst", null);
//        getString("#set($dic = ['A', 'B'])$dic.contains(\"$type\")", paramMap);
//        getString("#set($dic = ['A', 'B'])$dic.contains(\"$typeNull\")", paramMap);
    }

}
